// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Element/CHBDYGParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CHBDYGData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CHBDYG);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CHBDYG/CHBDYG.md>
*/
// 以下需要修改
enum CHBDYGKey : std::uint8_t
{
    // Row 1
    CHBDYG_EID     = 2,  // 表面元素识别编号
    CHBDYG_TYPE    = 4,  // 表面类型
    CHBDYG_IVIEWF  = 5,  // 前面的VIEW条目识别编号
    CHBDYG_IVIEWB  = 6,  // 后面的VIEW条目识别编号
    CHBDYG_RADMIDF = 7,  // 前面的RADM条目识别编号
    CHBDYG_RADMIDB = 8,  // 后面的RADM条目识别编号
};

namespace
{
    Common::I64 getType (const std::string& type)
    {
        static std::map<std::string, Common::I64> typeMap = { { "REV", 3 },   { "REV1", 11 }, { "AREA3", 4 },
                                                              { "AREA4", 5 }, { "AREA6", 8 }, { "AREA8", 9 } };
        auto                                      iter    = typeMap.find (type);
        if (iter == typeMap.end ())
        {
            return 1;
        }
        return typeMap[type];
    }
}  // namespace

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCHBDYG (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::CHBDYGData tempCHBDYG;
    tempCHBDYG.m_IVIEWF = tempCHBDYG.m_RADMIDF = 1;
    // TODO(bug 001) m_dominId的正确填写
    tempCHBDYG.m_domainId = 1;

    int curIDKey = 1;  // 当前字符串是第几项

    for (auto curStrKey : dividedEntryInfo)
    {
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        switch (curIDKey)
        {
        case CHBDYG_EID:
            tempCHBDYG.m_id = std::stoll (curStrKey);
            break;
        case CHBDYG_TYPE:
            boost::trim (curStrKey);
            tempCHBDYG.m_TYPE = getType (curStrKey);
            break;
        case CHBDYG_IVIEWF:
            tempCHBDYG.m_IVIEWF = std::stoll (curStrKey);
            break;
        case CHBDYG_IVIEWB:
            tempCHBDYG.m_IVIEWB = std::stoll (curStrKey);
            break;
        case CHBDYG_RADMIDF:
            tempCHBDYG.m_RADMIDF = std::stoll (curStrKey);
            break;
        case CHBDYG_RADMIDB:
            tempCHBDYG.m_RADMIDB = std::stoll (curStrKey);
            break;
        default:
            break;
        }

        curIDKey++;
    }

    for (size_t i = 0; i < 8; i++)
    {
        bool const isEmpty =
            std::all_of (dividedEntryInfo[11 + i].begin (), dividedEntryInfo[11 + i].end (), [] (char c) { return c == ' '; });
        if (isEmpty)
        {
            continue;
        }
        tempCHBDYG.m_g[i] = std::stoll (dividedEntryInfo[11 + i]);
    }

    return dbServiceFactory->get<IElementService> ()->Add (tempCHBDYG);
}
